package com.efeichong.project.controller;

import com.alibaba.excel.EasyExcel;
import com.efeichong.common.PageData;
import com.efeichong.common.domain.ResponseData;
import com.efeichong.project.domain.vo.SysPostVo;
import com.efeichong.project.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * @author
 * @date 2020/12/03
 * @description 岗位信息表
 */
@Api(tags = "岗位信息表 接口", value = "岗位信息表接口")
@RestController
@RequestMapping("/sysPost")
@RequiredArgsConstructor
public class SysPostController {

    private final SysPostService sysPostService;

    @ApiOperation(value = "岗位信息表详细")
    @GetMapping(value = "/getInfo")
    public ResponseData<SysPostVo> getInfo(@RequestParam("id") Long id) {
        return new ResponseData(sysPostService.getInfo(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "第几页开始", paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页行数", paramType = "query", dataTypeClass = Integer.class)
    })
    @ApiOperation(value = "岗位信息分页查询")
    @GetMapping("/page")
    public ResponseData<PageData<SysPostVo>> page(SysPostVo sysPostVo) {
        return new ResponseData(sysPostService.selectByPage(sysPostVo));
    }

    @ApiOperation(value = "岗位信息列表查询")
    @GetMapping("/list")
    public ResponseData<List<SysPostVo>> list(SysPostVo sysPostVo) {
        return new ResponseData(sysPostService.selectByList(sysPostVo));
    }

    @ApiOperation(value = "岗位信息表新增")
    @PostMapping("insert")
    public ResponseData insert(@RequestBody SysPostVo sysPostVo) {
        sysPostService.insertSysPost(sysPostVo);
        return new ResponseData();
    }

    @ApiOperation(value = "岗位信息表编辑")
    @PostMapping("update")
    public ResponseData update(@RequestBody SysPostVo sysPostVo) {
        sysPostService.updateSysPost(sysPostVo);
        return new ResponseData();
    }

    @ApiOperation(value = "岗位信息表批量删除")
    @GetMapping("/deleteByIds")
    public ResponseData deleteSysPostByIds(@RequestParam("ids") List<Long> ids) {
        sysPostService.deleteByIds(ids);
        return new ResponseData();
    }

    @ApiOperation(value = "岗位信息表删除")
    @GetMapping("/deleteById")
    public ResponseData deleteSysPostById(@RequestParam("id") Long id) {
        sysPostService.deleteById(id);
        return new ResponseData();
    }

    @SneakyThrows
    @ApiOperation(value = "导入", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @PostMapping(value = "/importExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseData importExcel(@RequestPart("file") MultipartFile file) {
        sysPostService.importExcel(file.getInputStream());
        return new ResponseData();
    }

    @ApiOperation(value = "导出", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping(value = "/exportExcel", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportExcel(SysPostVo sysPostVo,
                            HttpServletResponse response) {
        sysPostService.exportExcel(sysPostVo, response);
    }

    @SneakyThrows
    @ApiOperation(value = "模板下载", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping(value = "/downloadModel", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadModel(HttpServletResponse response) {
        EasyExcel.write().head(SysPostVo.class).file(response.getOutputStream()).sheet().doWrite(Collections.EMPTY_LIST);
    }
}
